﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

namespace 服务端
{
    class Program
    {
        static void Main(string[] args)
        {
            int recvLength;
            byte[] data = new byte[1024];
            IPEndPoint iend = new IPEndPoint(IPAddress.Any, 999);
            Socket newsock = new Socket(AddressFamily.InterNetwork, SocketType.Stream,
             ProtocolType.Tcp);
            newsock.Bind(iend);
            newsock.Listen(10);
            Console.WriteLine("waiting for a client");
            Socket client = newsock.Accept();
            IPEndPoint clientAdd = (IPEndPoint)client.RemoteEndPoint;
            Console.WriteLine("连接:" + clientAdd.Address + "端口：" + clientAdd.Port);
            string welcome = "welcome here!";
            data = Encoding.ASCII.GetBytes(welcome);
            client .Send (data,data.Length , SocketFlags.None );
            while (true )
            {
                data=new byte[1024];
                recvLength =client .Receive (data);
                if (recvLength ==0)
                break ;
                Console .WriteLine (Encoding .ASCII .GetString (data,0,recvLength ));
                client.Send(data,recvLength , SocketFlags.None );
            }
            Console.WriteLine("断开连接"+clientAdd .Address );
            client .Close ();
            newsock .Close ();
        }
    }
}
